import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { ApiService, JsonResult } from './api.service';
import { LicenseCount, PageVo, LicenseListVo, AddVo } from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})
export class LicenseService {

  constructor(
    private http: HttpClient,
    private apiService: ApiService
  ) { }

  getLicenseCount(): Observable<LicenseCount> {
    return this.apiService.get('/license/data/count')
      .pipe(
        map((jsonResult: JsonResult<LicenseCount>) => jsonResult.data)
      );
  }

  getLicenseList(pageIndex: number = 1, pageSize: number = 10): Observable<PageVo<LicenseListVo>> {
    const params = new HttpParams()
      .append('pageIndex', `${pageIndex}`)
      .append('pageSize', `${pageSize}`);
    return this.apiService.get('/license/data/list', { params })
      .pipe(
        map((jsonResult: JsonResult<PageVo<LicenseListVo>>) => jsonResult.data)
      );
  }

  addLicense(resData: any) {
    console.log('== resData ==', resData);
    return this.apiService.post('/license/data/add', resData).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult)
    );
  }

}
